using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.IO;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for VersionControl.
	/// </summary>
	public class VersionControl : AdminUserControl
	{
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label versionLabel;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label ownerLabel;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label publishedLabel;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label eworkVersionLabel;
    private System.Windows.Forms.ToolTip toolTip;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.TextBox descriptionLabel;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label sizeLabel;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.Label typeLabel;
    private System.ComponentModel.IContainer components;

		public VersionControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Apply changes", "Applies any changes made to this version", 
        new ActionExecuteDelegate(ApplyChanges));
      actionList.Add("Save...", "Save this version of the procedure/library", 
        new ActionExecuteDelegate(SaveVersion));
      actionList.Add("Open", "Open this version of the procedure/library in the Designer", 
        new ActionExecuteDelegate(OpenVersion));
      actionList.Add("Delete", "Delete this version of the procedure/library", 
        new ActionExecuteDelegate(DeleteVersion));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(VersionControl));
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.label1 = new System.Windows.Forms.Label();
      this.versionLabel = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.ownerLabel = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.publishedLabel = new System.Windows.Forms.Label();
      this.label4 = new System.Windows.Forms.Label();
      this.eworkVersionLabel = new System.Windows.Forms.Label();
      this.toolTip = new System.Windows.Forms.ToolTip(this.components);
      this.label5 = new System.Windows.Forms.Label();
      this.descriptionLabel = new System.Windows.Forms.TextBox();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.label6 = new System.Windows.Forms.Label();
      this.sizeLabel = new System.Windows.Forms.Label();
      this.label7 = new System.Windows.Forms.Label();
      this.typeLabel = new System.Windows.Forms.Label();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.SuspendLayout();
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(400, 8);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.TabIndex = 0;
      this.pictureBox1.TabStop = false;
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(88, 16);
      this.label1.TabIndex = 1;
      this.label1.Text = "Version number:";
      // 
      // versionLabel
      // 
      this.versionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.versionLabel.Location = new System.Drawing.Point(137, 8);
      this.versionLabel.Name = "versionLabel";
      this.versionLabel.Size = new System.Drawing.Size(255, 16);
      this.versionLabel.TabIndex = 2;
      this.versionLabel.Text = "versionLabel";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 24);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(48, 16);
      this.label2.TabIndex = 3;
      this.label2.Text = "Owner:";
      // 
      // ownerLabel
      // 
      this.ownerLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.ownerLabel.Location = new System.Drawing.Point(137, 24);
      this.ownerLabel.Name = "ownerLabel";
      this.ownerLabel.Size = new System.Drawing.Size(255, 16);
      this.ownerLabel.TabIndex = 4;
      this.ownerLabel.Text = "ownerLabel";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(8, 40);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(64, 16);
      this.label3.TabIndex = 5;
      this.label3.Text = "Published:";
      // 
      // publishedLabel
      // 
      this.publishedLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.publishedLabel.Location = new System.Drawing.Point(137, 40);
      this.publishedLabel.Name = "publishedLabel";
      this.publishedLabel.Size = new System.Drawing.Size(287, 16);
      this.publishedLabel.TabIndex = 6;
      this.publishedLabel.Text = "publishedLabel";
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(8, 56);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(132, 16);
      this.label4.TabIndex = 7;
      this.label4.Text = "Metastorm BPM Version:";
      // 
      // eworkVersionLabel
      // 
      this.eworkVersionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.eworkVersionLabel.Location = new System.Drawing.Point(134, 56);
      this.eworkVersionLabel.Name = "eworkVersionLabel";
      this.eworkVersionLabel.Size = new System.Drawing.Size(290, 16);
      this.eworkVersionLabel.TabIndex = 8;
      this.eworkVersionLabel.Text = "eworkVersionLabel";
      this.toolTip.SetToolTip(this.eworkVersionLabel, "This will be \'6\' for all version 6 procedures");
      // 
      // label5
      // 
      this.label5.Location = new System.Drawing.Point(8, 72);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(64, 16);
      this.label5.TabIndex = 9;
      this.label5.Text = "Description:";
      // 
      // descriptionLabel
      // 
      this.descriptionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.descriptionLabel.Location = new System.Drawing.Point(137, 72);
      this.descriptionLabel.Multiline = true;
      this.descriptionLabel.Name = "descriptionLabel";
      this.descriptionLabel.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
      this.descriptionLabel.Size = new System.Drawing.Size(295, 104);
      this.descriptionLabel.TabIndex = 10;
      this.descriptionLabel.Text = "descriptionLabel";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(442, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 392);
      this.uiActionsControl.TabIndex = 14;
      // 
      // label6
      // 
      this.label6.Location = new System.Drawing.Point(8, 184);
      this.label6.Name = "label6";
      this.label6.Size = new System.Drawing.Size(32, 16);
      this.label6.TabIndex = 15;
      this.label6.Text = "Size:";
      // 
      // sizeLabel
      // 
      this.sizeLabel.Location = new System.Drawing.Point(134, 184);
      this.sizeLabel.Name = "sizeLabel";
      this.sizeLabel.Size = new System.Drawing.Size(100, 16);
      this.sizeLabel.TabIndex = 16;
      this.sizeLabel.Text = "sizeLabel";
      // 
      // label7
      // 
      this.label7.Location = new System.Drawing.Point(8, 200);
      this.label7.Name = "label7";
      this.label7.Size = new System.Drawing.Size(72, 16);
      this.label7.TabIndex = 17;
      this.label7.Text = "Type:";
      // 
      // typeLabel
      // 
      this.typeLabel.Location = new System.Drawing.Point(134, 200);
      this.typeLabel.Name = "typeLabel";
      this.typeLabel.Size = new System.Drawing.Size(104, 16);
      this.typeLabel.TabIndex = 18;
      this.typeLabel.Text = "typeLabel";
      // 
      // VersionControl
      // 
      this.Controls.Add(this.typeLabel);
      this.Controls.Add(this.label7);
      this.Controls.Add(this.sizeLabel);
      this.Controls.Add(this.label6);
      this.Controls.Add(this.uiActionsControl);
      this.Controls.Add(this.descriptionLabel);
      this.Controls.Add(this.label5);
      this.Controls.Add(this.eworkVersionLabel);
      this.Controls.Add(this.label4);
      this.Controls.Add(this.publishedLabel);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.ownerLabel);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.versionLabel);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.pictureBox1);
      this.Name = "VersionControl";
      this.Size = new System.Drawing.Size(592, 392);
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.ResumeLayout(false);
      this.PerformLayout();

    }
		#endregion

    private FreeFlow.Administration.Version version;
    public virtual void LoadVersion(FreeFlow.Administration.Version version)
    {
      this.version = version;

      versionLabel.Text = version.VersionNumber.ToString();
      ownerLabel.Text = version.Owner;
      eworkVersionLabel.Text = version.EworkVersion.ToString();
      publishedLabel.Text = version.PublishedTime.ToString();
      descriptionLabel.Text = version.Description;
      sizeLabel.Text = version.Size.ToString() + " bytes";
      typeLabel.Text = version.Type;
    }

    private string ApplyChanges()
    {
      version.Description = descriptionLabel.Text;
      version.ApplyChanges();
      return Constants.ChangesApplied;
    }

    private string SaveVersion()
    {
      SaveVersion(version);
      return null;
    }

    public static void SaveVersion(FreeFlow.Administration.Version version)
    {
      using(SaveFileDialog dialog = new SaveFileDialog())
      {
        dialog.Filter = "Procedure files(*." + version.Type + ")|*." + version.Type;
        dialog.FileName = version.FileName;
        if (dialog.ShowDialog() == DialogResult.OK)
        {
          version.SaveToFile(dialog.FileName);
        }
      }
    }

    private string OpenVersion()
    {
      string fileName = Path.GetTempPath() + version.FileName;
      version.SaveToFile(fileName);
      Process.Start(fileName);
      return null;
    }

    private string DeleteVersion()
    {
      if (MessageBox.Show("Delete this version?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          version.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return null;
    }
	}
}
